import { Request, Response } from 'express';
import { dictionaryService } from './service';
import { Like } from 'typeorm';
import { ApiResponse } from '../../types/page';
import { DictionaryListResponse } from './types';

export const getDictionaries = async (req: Request, res: Response) => {
  try {
    const current = Number(req.query.page) || 1;
    const size = Number(req.query.size) || 10;
    
    const whereQuery: any = {};
    
    if (req.query.type) {
      whereQuery.type = req.query.type;
    }
    if (req.query.code) {
      whereQuery.code = Like(`%${req.query.code}%`);
    }
    if (req.query.name) {
      whereQuery.name = Like(`%${req.query.name}%`);
    }
    if (req.query.enable !== undefined) {
      whereQuery.enable = req.query.enable === 'true';
    }

    const { dictionaries, total } = await dictionaryService.findAll({
      ...whereQuery,
      page: current,
      pageSize: size,
    });
    
    const response: ApiResponse<DictionaryListResponse> = {
      success: true,
      data: {
        records: dictionaries,
        total,
        size,
        current,
        pages: Math.ceil(total / size)
      }
    };

    res.json(response);
  } catch (error: any) {
    res.json({
      success: false,
      message: '获取字典列表失败',
      error: error.message
    });
  }
};

export const getDictionaryById = async (req: Request, res: Response) => {
  try {
    const dictionary = await dictionaryService.findById(Number(req.params.id));
    if (!dictionary) {
      return res.status(404).json({ success: false, message: '字典项不存在' });
    }
    res.json({ success: true, data: dictionary });
  } catch (error) {
    res.status(500).json({ success: false, message: '获取字典项失败', error });
  }
};

export const getDictionaryByType = async (req: Request, res: Response) => {
  try {
    const dictionaries = await dictionaryService.findByType(req.params.type);
    res.json({ success: true, data: dictionaries });
  } catch (error) {
    res.status(500).json({ success: false, message: '获取字典值列表失败', error });
  }
};

export const createDictionary = async (req: Request, res: Response) => {
  try {
    const dictionary = await dictionaryService.create(req.body);
    res.status(201).json({ success: true, data: dictionary });
  } catch (error: any) {
    res.status(error.status || 500).json({ 
      success: false, 
      message: error.message || '创建字典项失败'
    });
  }
};

export const updateDictionary = async (req: Request, res: Response) => {
  try {
    const dictionary = await dictionaryService.update(Number(req.params.id), req.body);
    if (!dictionary) {
      return res.status(404).json({ success: false, message: '字典项不存在' });
    }
    res.json({ success: true, data: dictionary });
  } catch (error: any) {
    res.status(error.status || 500).json({ 
      success: false, 
      message: error.message || '更新字典项失败'
    });
  }
};

export const deleteDictionary = async (req: Request, res: Response) => {
  try {
    const result = await dictionaryService.delete(Number(req.params.id));
    if (!result) {
      return res.status(404).json({ success: false, message: '字典项不存在' });
    }
    res.json({ success: true, message: '删除成功' });
  } catch (error) {
    res.status(500).json({ success: false, message: '删除字典项失败', error });
  }
};

export const getDictionaryTypes = async (req: Request, res: Response) => {
  try {
    const types = await dictionaryService.findUniqueTypes();
    res.json({ 
      success: true, 
      data: types 
    });
  } catch (error) {
    res.status(500).json({ 
      success: false, 
      message: '获取字典类型列表失败', 
      error 
    });
  }
}; 